﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using nRoute.Components;
using nRoute.Samples.Netflix.Data.Catalog;
using System.Text.RegularExpressions;

namespace nRoute.Samples.Netflix.Views
{
    /// <summary>
    /// Interaction logic for TitleView.xaml
    /// </summary>
    public partial class TitleView : UserControl
    {
        private const string SEPERATOR = " /";
        private const string RUNTIME_FORMAT = "{0} hrs {1} mins";
        private const string HTML_TAG_PATTERN = "<.*?>";

        public TitleView()
        {
            InitializeComponent();

            this.SetRelayConverter<int?, string>("SecondsToTimeConverter", (i) =>
            {
                if (!i.HasValue) return "Unspecified Runtime";
                var _time = TimeSpan.FromSeconds(i.Value);
                return string.Format(RUNTIME_FORMAT, _time.Hours, _time.Minutes);
            });

            this.SetRelayConverter<string, string>("StripHTMLConverter", (s) => Regex.Replace(s, HTML_TAG_PATTERN, string.Empty));

            this.SetRelayConverter<BoxArt, string>("BoxArtUrlConverter", (b) => b.HighDefinitionUrl ?? b.LargeUrl ?? b.MediumUrl);

            this.SetRelayConverter<double?, double>("RatingToWidthConverter", (d) => d.HasValue ? d.Value * 19 : 0d);
        }
    }
}
